import { createRouter, createWebHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'

const routes: RouteRecordRaw[] = [
  {
    path: '/',
    name: 'Home',
    component: () => import('../views/Home.vue'),
    meta: {
      title: '首页',
      keepAlive: true
    }
  },
  {
    path: '/category',
    name: 'Category',
    component: () => import('../views/Category.vue'),
    meta: {
      title: '分类',
      keepAlive: true
    }
  },
  {
    path: '/cart',
    name: 'Cart',
    component: () => import('../views/Cart.vue'),
    meta: {
      title: '购物车',
      keepAlive: false
    }
  },
  {
    path: '/profile',
    name: 'Profile',
    component: () => import('../views/Profile.vue'),
    meta: {
      title: '我的',
      keepAlive: true
    }
  },
  {
    path: '/pht',
    name: 'Pht',
    component: () => import('../views/Pht.vue'),
    meta: {
      title: 'pht',
      keepAlive: false
    }
  },
  {
    path: '/product/:id',
    name: 'ProductDetail',
    component: () => import('../views/ProductDetail.vue'),
    meta: {
      title: '商品详情',
      keepAlive: false
    }
  },
  {
    path: '/tabs',
    name: 'TabsDemo',
    component: () => import('../views/TabsDemo.vue'),
    meta: {
      title: 'Tabs',
      keepAlive: false
    }
  },
  {
    path: '/popup',
    name: 'PopupDemo',
    component: () => import('../views/PopupDemo.vue'),
    meta: {
      title: 'Popup',
      keepAlive: false
    }
  },
  {
    path: '/category/:id',
    name: 'CategoryDetail',
    component: () => import('../views/CategoryDetail.vue'),
    meta: {
      title: '分类详情',
      keepAlive: false
    }
  },
  {
    path: '/waterfall',
    name: 'WaterfallDemo',
    component: () => import('../views/WaterfallDemo.vue'),
    meta: {
      title: '瀑布流商品列表',
      keepAlive: false
    }
  },
  {
    path: '/ai-chat',
    name: 'AiChat',
    component: () => import('../views/AiChat.vue'),
    meta: {
      title: 'AI助手',
      keepAlive: false
    }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0 }
    }
  }
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  if (to.meta?.title) {
    document.title = `${to.meta.title} - 移动商城`
  }
  next()
})

export default router